var pathTool = require('path');
var webpack = require('webpack');
var ExtractTextPlugin = require('extract-text-webpack-plugin'); //css单独打包
var CleanWebpackPlugin = require('clean-webpack-plugin');
var HtmlWebpackPlugin = require('html-webpack-plugin');
var HtmlWebpackHarddiskPlugin = require('html-webpack-harddisk-plugin');

const api_host = '';

module.exports = {
    entry: {
        app: ['babel-polyfill', './modules/index.js']
    },
    resolve: {
        modules: [pathTool.resolve(__dirname, 'common'), pathTool.resolve(__dirname, 'util'), 'node_modules'],
        extensions: ['.js', '.json', '.jsx'],
        mainFiles: ['index']
    },
    output: {
        //输出信息
        path: pathTool.resolve(__dirname, './build'), //线上路径'./build/'
        filename: 'storeadmin-[name]-[chunkhash].js',
        chunkFilename: 'storeadmin-[name]-[id].[chunkhash:8].bundle.js',
        publicPath: '/build/'
    },
    module: {
        //处理jsx的编译
        rules: [
            {
                test: /\.(js|jsx)$/,
                exclude: [pathTool.resolve(__dirname, 'node_modules')],
                enforce: 'pre',
                use: {
                    loader: 'babel-loader',
                    options: {
                        plugins: [
                            ['import', [{ libraryName: "antd", style: 'css' }]],
                        ],
                        cacheDirectory: true
                    }
                }
            },
            {
                test: /\.(css|scss)$/,
                use: ExtractTextPlugin.extract({
                    use: [
                        {
                            loader: 'css-loader'
                        },
                        {
                            loader: 'sass-loader'
                        }
                    ],
                    fallback: 'style-loader',
                    publicPath: '/build/'
                })
            }
        ]
    },
    plugins: [
        new CleanWebpackPlugin('./build/*.*'),
        new webpack.DefinePlugin({
            api_host: JSON.stringify(api_host)
        }),
        new HtmlWebpackPlugin({
            alwaysWriteToDisk: true,
            template: './views/main.ejs',
            filename: pathTool.resolve(__dirname, './main.html')
        }),
        new HtmlWebpackHarddiskPlugin(),
        new ExtractTextPlugin({
            filename: 'style.css',
            disable: false,
            allChunks: true
        }),
        new webpack.optimize.ModuleConcatenationPlugin(),
        new webpack.optimize.UglifyJsPlugin({
        	sourceMap: false,
        	beautify: false,
        	mangle: {
        		screw_ie8: false,
        		keep_fnames: false
        	},
        	compress: {
        		screw_ie8: false,
        		warnings: false,
        		drop_console: true
        	},
        	comments: false
        })
    ]
};
